﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Truextend.CoffeShop.Bussiness;
using Truextend.CoffeShop.Model;
using Truextend.CoffeShop.Service;

namespace Truextend.CoffeShop.Presenter.Orders
{
    public class OrdersMenuPresenter
    {
        IOrdersMenu view;

        public OrdersMenuPresenter(IOrdersMenu view) {
            this.view = view;
        }

        public void FillOrdersInProgress(string state)
        {
            OrderService orderService = new OrderService();
            RestaurantService restaurantService = new RestaurantService();
            ConsumptionService consumptionService = new ConsumptionService();
            DetailService detailService = new DetailService();

            IList<Order> list_orders = orderService.GetOrdersForState(state);

            DataTable dataTable = new DataTable();

            dataTable.Columns.Add("OrderID");
            dataTable.Columns.Add("Title");
            dataTable.Columns.Add("Restaurant");
            dataTable.Columns.Add("StartDate");
            dataTable.Columns.Add("EndDate");
            dataTable.Columns.Add("Action");
            dataTable.Columns.Add("Delete", typeof(bool));

            foreach (var order in list_orders)
            {
                Restaurant restaurant = new Restaurant();
                
                restaurant = restaurantService.GetRestaurantByOrderId(order.OrderId);

                DataRow dataRow = dataTable.NewRow();
                dataRow["OrderID"] = order.OrderId;
                dataRow["Title"] = order.Title;
                dataRow["Restaurant"] = restaurant.Name;
                dataRow["StartDate"] = order.StartDate;
                dataRow["EndDate"] = order.EndDate;

                IList<Detail> allDetailsOfAnOrder = detailService.GetAllDetailsOfAnOrder(order.OrderId);

                foreach (var detail in allDetailsOfAnOrder)
                {
                    if (consumptionService.GetConsumptionForDetailAndUserID(detail.DetailId,view.UserId) != null)
                    {
                        dataRow["Action"] = "Edit";
                        dataRow["Delete"] = true;
                        break;
                    }

                    dataRow["Action"] = "Join";
                    dataRow["Delete"] = false;
                }                
                
                
                dataTable.Rows.Add(dataRow);
            }

            view.orders = dataTable;        
        }

        public void FillOrders(string state)
        {
            OrderService orderService = new OrderService();
            RestaurantService restaurantService = new RestaurantService();

            IList<Order> list_orders = orderService.GetOrdersForState(state);                            

            DataTable data_table = new DataTable();

            data_table.Columns.Add("OrderID");
            data_table.Columns.Add("Title");
            data_table.Columns.Add("Restaurant");
            data_table.Columns.Add("StartDate");
            data_table.Columns.Add("EndDate");                

            foreach(var order in list_orders)
            {
                Restaurant restaurant = new Restaurant();                

                restaurant = restaurantService.GetRestaurantByOrderId(order.OrderId);                                

                DataRow data_row = data_table.NewRow();
                data_row["OrderID"] = order.OrderId;
                data_row["Title"] = order.Title;
                data_row["Restaurant"] = restaurant.Name;
                data_row["StartDate"] = order.StartDate;
                data_row["EndDate"] = order.EndDate;

                data_table.Rows.Add(data_row);
            }

            view.orders = data_table;        

        }

        public void CancelOrder(int orderId)
        {
            DetailService detailService = new DetailService();
            ConsumptionService consumptionService = new ConsumptionService();

            IList<Detail> listDetails = detailService.GetAllDetailsOfAnOrder(orderId);

            foreach (var detail in listDetails)
            {
                Consumption consumption = consumptionService.GetConsumptionForDetailAndUserID(detail.DetailId, view.UserId);
                if (consumption != null)
                {
                    consumptionService.DeleteConsumption(consumption);
                }
            }
        }
    }
}
